package com.school.sports.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.school.sports.entity.SysDictData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 字典数据Mapper接口
 */
@Mapper
public interface SysDictDataMapper extends BaseMapper<SysDictData> {

    /**
     * 根据字典类型ID查询字典数据列表
     * @param dictTypeId 字典类型ID
     * @return 字典数据列表
     */
    @Select("SELECT dd.*, dt.dict_name as dict_type_name, dt.dict_type as dict_type_code " +
            "FROM sys_dict_data dd " +
            "LEFT JOIN sys_dict_type dt ON dd.dict_type_id = dt.id " +
            "WHERE dd.dict_type_id = #{dictTypeId} AND dd.status = 1 AND dd.deleted = 0 " +
            "ORDER BY dd.dict_sort ASC, dd.created_at ASC")
    List<SysDictData> selectByDictTypeId(@Param("dictTypeId") Long dictTypeId);

    /**
     * 根据字典类型编码查询字典数据列表
     * @param dictTypeCode 字典类型编码
     * @return 字典数据列表
     */
    @Select("SELECT dd.*, dt.dict_name as dict_type_name, dt.dict_type as dict_type_code " +
            "FROM sys_dict_data dd " +
            "LEFT JOIN sys_dict_type dt ON dd.dict_type_id = dt.id " +
            "WHERE dt.dict_type = #{dictTypeCode} AND dd.status = 1 AND dd.deleted = 0 " +
            "ORDER BY dd.dict_sort ASC, dd.created_at ASC")
    List<SysDictData> selectByDictTypeCode(@Param("dictTypeCode") String dictTypeCode);

    /**
     * 根据字典类型编码和字典值查询字典数据
     * @param dictTypeCode 字典类型编码
     * @param dictValue 字典值
     * @return 字典数据对象
     */
    @Select("SELECT dd.*, dt.dict_name as dict_type_name, dt.dict_type as dict_type_code " +
            "FROM sys_dict_data dd " +
            "LEFT JOIN sys_dict_type dt ON dd.dict_type_id = dt.id " +
            "WHERE dt.dict_type = #{dictTypeCode} AND dd.dict_value = #{dictValue} " +
            "AND dd.status = 1 AND dd.deleted = 0 " +
            "LIMIT 1")
    SysDictData selectByDictTypeCodeAndValue(@Param("dictTypeCode") String dictTypeCode,
                                           @Param("dictValue") String dictValue);

    /**
     * 根据字典类型编码和字典标签查询字典数据
     * @param dictTypeCode 字典类型编码
     * @param dictLabel 字典标签
     * @return 字典数据对象
     */
    @Select("SELECT dd.*, dt.dict_name as dict_type_name, dt.dict_type as dict_type_code " +
            "FROM sys_dict_data dd " +
            "LEFT JOIN sys_dict_type dt ON dd.dict_type_id = dt.id " +
            "WHERE dt.dict_type = #{dictTypeCode} AND dd.dict_label = #{dictLabel} " +
            "AND dd.status = 1 AND dd.deleted = 0 " +
            "LIMIT 1")
    SysDictData selectByDictTypeCodeAndLabel(@Param("dictTypeCode") String dictTypeCode,
                                            @Param("dictLabel") String dictLabel);

    /**
     * 检查字典值是否在指定字典类型中存在
     * @param dictTypeId 字典类型ID
     * @param dictValue 字典值
     * @param excludeId 排除的ID（用于更新时检查）
     * @return 数量
     */
    @Select("SELECT COUNT(1) FROM sys_dict_data WHERE dict_type_id = #{dictTypeId} AND dict_value = #{dictValue} " +
            "AND deleted = 0 AND (#{excludeId} IS NULL OR id != #{excludeId})")
    int countByDictTypeIdAndValue(@Param("dictTypeId") Long dictTypeId,
                                 @Param("dictValue") String dictValue,
                                 @Param("excludeId") Long excludeId);

    /**
     * 查询所有启用的字典数据（用于缓存）
     * @return 字典数据列表
     */
    @Select("SELECT dd.*, dt.dict_name as dict_type_name, dt.dict_type as dict_type_code " +
            "FROM sys_dict_data dd " +
            "LEFT JOIN sys_dict_type dt ON dd.dict_type_id = dt.id " +
            "WHERE dd.status = 1 AND dd.deleted = 0 AND dt.status = 1 AND dt.deleted = 0 " +
            "ORDER BY dt.dict_type, dd.dict_sort ASC")
    List<SysDictData> selectAllEnabledDictData();

    /**
     * 根据标签模糊查询字典数据
     * @param dictLabel 字典标签
     * @return 字典数据列表
     */
    @Select("SELECT dd.*, dt.dict_name as dict_type_name, dt.dict_type as dict_type_code " +
            "FROM sys_dict_data dd " +
            "LEFT JOIN sys_dict_type dt ON dd.dict_type_id = dt.id " +
            "WHERE dd.dict_label LIKE CONCAT('%', #{dictLabel}, '%') " +
            "AND dd.status = 1 AND dd.deleted = 0 " +
            "ORDER BY dt.dict_type, dd.dict_sort ASC")
    List<SysDictData> selectByDictLabelLike(@Param("dictLabel") String dictLabel);
}